﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace OldFactory
{
    public class Configs
    {
        private string ConfigFileName = "";
        public string IPAddress { get; set; } = "172.16.1.85";
        public int IPPort { get; set; } = 102;
        
        public void Init()
        {
            ConfigFileName = Path.Combine(Environment.CurrentDirectory, "config.json");
            try
            {
                string json = File.ReadAllText(ConfigFileName, Encoding.UTF8);
                var cfg = JsonSerializer.Deserialize<Configs>(json) ?? new Configs();
                IPAddress = cfg.IPAddress;
                IPPort = cfg.IPPort;
            }
            catch
            {
                IPAddress = "172.16.1.85";
                IPPort = 102;
            }
        }
        public void Save()
        {
            string json = JsonSerializer.Serialize(this);
            File.WriteAllText(ConfigFileName, json, Encoding.UTF8);
        }
    }
}
